package com.telit.firecommand.controller;


import com.telit.common.core.utils.FeignUtils;
import com.telit.common.core.utils.Result;
import com.telit.common.entity.DataReturn;
import com.telit.upms.feign.RemoteUpmsService;
import com.telit.upms.vo.MicroSysDeptTreeOptionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 组织架构API
 *
 * @author yuezhuang
 * @date 2022-01-13
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/sys-dept", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "组织架构", value = "组织架构", produces = MediaType.APPLICATION_JSON_VALUE)
public class SysDeptController {

    private final RemoteUpmsService remoteUpmsService;

    /**
     * 微服务内部调用->组织架构->根据传入行政区划获取该行政区划下所有部门
     *
     * @param code 行政区划编码
     * @param flag 行政区划是否向下递归 true=向下递归，false=不向下递归
     *
     * @return 查询结果集
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "行政区划编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "flag", value = "行政区划是否向下递归 true=向下递归，false=不向下递归", required = true, paramType = "query", dataType = "String"),
    })
    @ApiOperation("根据传入行政区划获取该行政区划下所有部门")
    @RequestMapping(method = RequestMethod.GET, value = "selectDeptInfoDetailForSelectOption")
    public DataReturn<List<MicroSysDeptTreeOptionVo>> list(@RequestParam("code") String code,
                                                           @RequestParam("flag") Boolean flag) {
        Result<List<MicroSysDeptTreeOptionVo>> listResult = remoteUpmsService.selectDeptInfoByRegionCodeForSelectOption(
                code, flag);
        List<MicroSysDeptTreeOptionVo> microSysDeptTreeOptionVos = FeignUtils.unwrap(listResult);

        return DataReturn.ok(microSysDeptTreeOptionVos);
    }

}
